#include<opencv2/opencv.hpp>
#include<curveMatch.h>
#include<iostream>
#include<math.h>

using namespace cv;
using namespace std;

int main()
{
	//freopen("curvePoint.txt", "w", stdout);
	setNumThreads(32);
	std::cout << "Thread Num=" << getNumThreads() << endl;
	Mat template_curve = imread("template\\1.bmp", IMREAD_GRAYSCALE);
	Mat source_img = imread("image\\1_crop2.jpg");
	//pyrDown(source_img, source_img, Size(source_img.cols / 2, source_img.rows / 2));
	pyrDown(template_curve, template_curve, Size(template_curve.cols / 2, template_curve.rows / 2));
	curveMatch cm(source_img, template_curve);
	cm.match();
	cm.seal();
	namedWindow("resault", WINDOW_NORMAL);
	imshow("resault", cm.srcMat);
	waitKey(0);
	imwrite("match_resault.jpg", cm.srcMat);

	//Mat canvas = Mat::zeros(Size(1000, 1000), CV_8UC3);
	//Mat mask = Mat::zeros(canvas.size(), CV_8UC1);
	//canvas = Scalar::all(255);
	//vector<Mat>bgr(3);
	//split(canvas, bgr);
	//freopen("curvePoint.txt", "r", stdin);
	//vector<Point>fPoints;
	//vector<Point>srcPoints;
	//int count = 0;
	//int x = 0, y = 0;
	//cin >> x >> y;
	//while (x >= 0 && y >= 0)
	//{
	//	count++;
	//	if (count % 10 == 0)
	//	{
	//		fPoints.push_back(Point(x, y));
	//		bgr[0].at<uchar>(y, x) = 0;
	//	}
	//	cin >> x >> y;
	//}
	//bgr[0] = 255 - bgr[0];
	//Rect rect(0, 0, canvas.cols, canvas.rows);
	//MemStorage* storage;
	//Subdiv2D* subDiv;


	//freopen("srcPoint.txt", "r", stdin);
	//count = 0;
	//cin >> x >> y;
	//while (x >= 0 && y >= 0)
	//{
	//	count++;
	//	if (count % 10 == 0)
	//	{
	//		bgr[1].at<uchar>(y, x) = 0;
	//		srcPoints.push_back(Point(x, y));
	//		if (srcPoints.size() == fPoints.size())
	//			break;
	//	}
	//	cin >> x >> y;
	//}
	//while (fPoints.size() > srcPoints.size())
	//	fPoints.erase(fPoints.begin() + fPoints.size() - 1);
	//bgr[1] = 255 - bgr[1];
	//bgr[2] = Scalar::all(0);

	//cv::Ptr <cv::ShapeContextDistanceExtractor> mysc = cv::createShapeContextDistanceExtractor();

	//SCD set1SCE(12, 4, 0.2f, 2), set2SCE(12, 4, 0.2f, 2);
	//Mat set1SCD, set2SCD;
	//InputArray contour1 = fPoints;
	//InputArray contour2 = srcPoints;
	//Mat sset1 = contour1.getMat(), sset2 = contour2.getMat(), set1, set2;
	//if (set1.type() != CV_32F)
	//	sset1.convertTo(set1, CV_32F);
	//else
	//	sset1.copyTo(set1);

	//if (set2.type() != CV_32F)
	//	sset2.convertTo(set2, CV_32F);
	//else
	//	sset2.copyTo(set2);
	//if (set1.dims > 1)
	//	set1 = set1.reshape(2, 1);
	//if (set2.dims > 1)
	//	set2 = set2.reshape(2, 1);
	//SCDMatcher matcher;
	//vector<DMatch>matches;
	//float sDistance = 0, bEnergy = 0, iAppearance = 0;
	//float beta;

	//// Initializing some variables //
	//std::vector<int> inliers1, inliers2;
	//Ptr<ShapeTransformer> transformer;
	//Ptr<HistogramCostExtractor> comparer;
	//Ptr<ThinPlateSplineShapeTransformer> transDown = transformer.dynamicCast<ThinPlateSplineShapeTransformer>();
	//Mat warpedImage;

	//set1SCE.extractSCD(set1, set1SCD, inliers1);
	//// Extract SCD descriptor of the set2 (TARGET) //
	//set2SCE.extractSCD(set2, set2SCD, inliers2, set1SCE.getMeanDistance());
	//matcher.matchDescriptors(set1SCD, set2SCD, matches, comparer, inliers1, inliers2);

	//merge(bgr, canvas);
	//namedWindow("canvas", WINDOW_NORMAL);
	//imshow("canvas", canvas);
	//waitKey(0);
	return 0;
}